package com.ceiling.controller;

import com.ceiling.bean.BaseRespVo;
import com.ceiling.bean.promotion.Coup;
import com.ceiling.bean.promotion.ads.bo.AdsCreateBO;
import com.ceiling.bean.promotion.ads.bo.AdsDelBO;
import com.ceiling.bean.promotion.ads.bo.AdsEditBO;
import com.ceiling.bean.promotion.ads.bo.AdsShowListBO;
import com.ceiling.bean.promotion.ads.vo.AdsCreatedByAddPicVO;
import com.ceiling.bean.promotion.ads.vo.AdsCreatedVO;
import com.ceiling.bean.promotion.ads.vo.AdsShowListVO;
import com.ceiling.bean.promotion.ads.vo.AdsUltimateShowListVO;
import com.ceiling.bean.promotion.coup.bo.CoupShowListBO;
import com.ceiling.bean.promotion.coup.vo.CoupUltimateShowListVO;
import com.ceiling.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("admin")
public class PromotionController {

    @Autowired
    PromotionService promotionService;

    /*----------------------------广告管理-------------------------------------*/

    /**
     * 更具页面和显示数量显示广告
     * @param adsShowListBO
     * @return 返回json请求数据
     */
    @RequestMapping(value = "ad/list",method = RequestMethod.GET)
    public BaseRespVo adShowList(AdsShowListBO adsShowListBO){

        AdsUltimateShowListVO adShowList =promotionService.adShowList(adsShowListBO);

        BaseRespVo ok = BaseRespVo.ok(adShowList);


        return ok;
    }

    /**
     * 存储图片并返回图片信息
     * @param file
     * @param request
     * @return 返回存储的图片信息
     */
    @RequestMapping(value = "storage/create",method = RequestMethod.POST)
    public BaseRespVo adCreatePic(MultipartFile file, HttpServletRequest request){
        //获取本地url：http://localhost:8083
        String url = request.getRequestURL().toString();
        String replace = url.replace(request.getRequestURI(), "");

        //存储图片
        AdsCreatedByAddPicVO addPicVO=promotionService.adCreatePic(file,replace);

        if(addPicVO==null){
            return BaseRespVo.fail("照片损坏！");
        }else{
            return BaseRespVo.ok(addPicVO);
        }
    }

    /**
     * 添加一条广告
     * @param adsCreateBO
     * @return 返回添加已添加的信息
     */
    @RequestMapping(value = "ad/create",method = RequestMethod.POST)
    public BaseRespVo adCreated(@RequestBody AdsCreateBO adsCreateBO){

        AdsCreatedVO adsCreatedVO=promotionService.adCreated(adsCreateBO);

        if(adsCreatedVO==null){
            return BaseRespVo.fail("添加失败！");
        }else{
            return BaseRespVo.ok(adsCreatedVO);
        }
    }

    /**
     * 修改广告
     * @param adsEditBO
     * @return
     */
    @RequestMapping(value = "ad/update",method = RequestMethod.POST)
    public BaseRespVo adEdit(@RequestBody AdsEditBO adsEditBO){

        AdsCreatedVO adEdit=promotionService.adEdit(adsEditBO);


        if(adEdit==null){
            return BaseRespVo.fail("修改失败！");
        }else{
            return BaseRespVo.ok(adEdit);
        }
    }

    /**
     * 删除广告(加删除修改enabled以及delete)
     * @param adsDelBO
     * @return
     */
    @RequestMapping(value = "ad/delete",method = RequestMethod.POST)
    public BaseRespVo adDel(@RequestBody AdsDelBO adsDelBO){

        Integer affactRows=promotionService.adDel(adsDelBO);

        if(affactRows==null){
            return BaseRespVo.fail("删除失败！");
        }else{
            return BaseRespVo.ok();
        }
    }

    /*----------------------------优惠卷管理-------------------------------------*/

    /**
     * 获取优惠卷
     * @param coupShowListBO
     * @return
     */
    @RequestMapping(value = "coupon/list",method = RequestMethod.GET)
    public BaseRespVo coupShowList(CoupShowListBO coupShowListBO){

        CoupUltimateShowListVO coupShowList =promotionService.coupShowList(coupShowListBO);

        BaseRespVo ok = BaseRespVo.ok(coupShowList);


        return ok;
    }

    /**
     * 编辑优惠卷
     * @param coup
     * @return
     */
    @RequestMapping(value = "coupon/update",method = RequestMethod.POST)
    public BaseRespVo coupEdit(@RequestBody Coup coup){
        Coup coupVO=promotionService.coupEdit(coup);

        if(coupVO==null){
            return BaseRespVo.fail("编辑失败！");
        }else{
            return BaseRespVo.ok(coup);
        }
    }


    /**
     * 删除优惠卷（假删除）
     * @param coup
     * @return
     */
    @RequestMapping(value = "coupon/delete",method = RequestMethod.POST)
    public BaseRespVo coupDel(@RequestBody Coup coup){
        Integer affectRows=promotionService.coupDel(coup);

        if(affectRows==null){
            return BaseRespVo.fail("删除失败！");
        }else{
            return BaseRespVo.ok();
        }
    }

    @RequestMapping(value = "coupon/create",method = RequestMethod.POST)
    public BaseRespVo coupCreated(@RequestBody Coup coup){

        coup=promotionService.coupCreated(coup);

        if(coup==null){
            return BaseRespVo.fail("删除失败！");
        }else{
            return BaseRespVo.ok();
        }
    }





    /*----------------------------广告管理-------------------------------------*/

    /*----------------------------广告管理-------------------------------------*/

    /*----------------------------广告管理-------------------------------------*/

    /*----------------------------广告管理-------------------------------------*/

    /*----------------------------广告管理-------------------------------------*/

}
